package com.demo.mockito;

import com.demo.web.HelloController;
import org.junit.jupiter.api.*;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class CaptorTest {

    @Mock
    List<String> stringList;

    @Mock
    HelloController helloController;

    @Captor
    ArgumentCaptor<String> argumentCaptor;

    private AutoCloseable closeable;
    @BeforeEach
    public void setup() {
        System.out.println("Before each test");
        closeable = MockitoAnnotations.openMocks(this);
    }
    @AfterEach
    public void release() throws Exception {
        System.out.println("After each test");
        closeable.close();
    }

    @BeforeAll
    public static void beforeAllTest() {
        System.out.println("Before all test");
    }

    @AfterAll
    public static void afterAllTest() {
        System.out.println("After all test");
    }

    @Test
    public void test() {
        when(helloController.sayHello()).thenReturn("Hello, World!");
        stringList.add(helloController.sayHello());
        // it is recommended to use capture() only with verify()
        verify(stringList).add(argumentCaptor.capture());
        // 只有在使用capture()方法后才能使用getValue()方法
        assertEquals("Hello, World!", argumentCaptor.getValue());
    }

    @Test
    public void test1() {
        System.out.println("Test 1");
    }

}
